/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUISale.java
 *
 * Created on 07-04-2011, 19:05:26
 */

package GUILayer;

import ControlLayer.CtrCustomer;
import ControlLayer.CtrInvoice;
import ControlLayer.CtrProduct;
import ControlLayer.CtrSalesLine;
import ControlLayer.CtrSalesOrder;
import DBLayer.GetMax;
import ModelLayer.Customer;
import ModelLayer.Invoice;
import ModelLayer.Product;
import ModelLayer.SalesLine;
import ModelLayer.SalesOrder;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Henrik-iMac
 */
public class GUISale extends javax.swing.JFrame {

    /** Creates new form GUISale */
    public GUISale() {
        initComponents();
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        partNoField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        amountField = new javax.swing.JTextField();
        enterItemButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        itemLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel9 = new javax.swing.JLabel();
        totalOrderPriceLabel = new javax.swing.JLabel();
        amountLabel = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        searchCustIdField = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        searchCustName = new javax.swing.JTextField();
        searchCustButton = new javax.swing.JButton();
        searchCustStatusLabel = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jPanel5 = new javax.swing.JPanel();
        printInvoiceButton = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Scan item here"));

        jLabel1.setText("Part#");

        jLabel2.setText("Amount");

        enterItemButton.setText("Enter item");
        enterItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enterItemButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(34, 34, 34)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(amountField)
                            .addComponent(partNoField, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)))
                    .addComponent(enterItemButton))
                .addContainerGap(61, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(partNoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(amountField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(enterItemButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Scanned items"));

        itemLabel.setText("Item");

        jLabel4.setText("Price");

        jLabel5.setText("Amount");

        jLabel6.setText("Total");

        jList1.setFont(new java.awt.Font("Courier New", 0, 11));
        jScrollPane1.setViewportView(jList1);

        jLabel9.setText("Total");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(itemLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addGap(45, 45, 45)
                .addComponent(jLabel5)
                .addGap(65, 65, 65)
                .addComponent(jLabel6)
                .addGap(122, 122, 122))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addComponent(amountLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 206, Short.MAX_VALUE)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(totalOrderPriceLabel)
                .addGap(149, 149, 149))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 414, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(itemLabel)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 422, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(totalOrderPriceLabel)
                    .addComponent(amountLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Customer info"));

        jLabel7.setText("Id");

        jLabel8.setText("Name");

        searchCustButton.setText("search");
        searchCustButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchCustButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addGap(32, 32, 32)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(searchCustIdField, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(searchCustName, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)))
                    .addComponent(searchCustButton)
                    .addComponent(searchCustStatusLabel))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(searchCustIdField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(searchCustName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(searchCustButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(searchCustStatusLabel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Invoice"));

        jList2.setFont(new java.awt.Font("Courier New", 0, 11));
        jScrollPane2.setViewportView(jList2);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE)
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Done scanning items"));

        printInvoiceButton.setText("Print invoice");
        printInvoiceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printInvoiceButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(printInvoiceButton)
                .addContainerGap(96, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(printInvoiceButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jMenu1.setText("Navigation");

        jMenuItem1.setText("Main menu");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(109, 109, 109)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(39, 39, 39))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        setVisible(false);
        GUIMainMenu gui = new GUIMainMenu();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void enterItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enterItemButtonActionPerformed
        // TODO add your handling code here:
        enterItem();
    }//GEN-LAST:event_enterItemButtonActionPerformed

    private void searchCustButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchCustButtonActionPerformed
        // TODO add your handling code here:
        searchCustomer();
    }//GEN-LAST:event_searchCustButtonActionPerformed

    private void printInvoiceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printInvoiceButtonActionPerformed
        // TODO add your handling code here:
        printInvoice();
    }//GEN-LAST:event_printInvoiceButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUISale().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField amountField;
    private javax.swing.JLabel amountLabel;
    private javax.swing.JButton enterItemButton;
    private javax.swing.JLabel itemLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField partNoField;
    private javax.swing.JButton printInvoiceButton;
    private javax.swing.JButton searchCustButton;
    private javax.swing.JTextField searchCustIdField;
    private javax.swing.JTextField searchCustName;
    private javax.swing.JLabel searchCustStatusLabel;
    private javax.swing.JLabel totalOrderPriceLabel;
    // End of variables declaration//GEN-END:variables

    int invoiceNo;
    int salesOrderId;
    int customerId;
    double totalOrderPrice = 0;
    int totalAmount = 0;

    

    private void createNewOrder(){
        CtrInvoice ctrInvoice = new CtrInvoice();
        Invoice invoiceObj = new Invoice();
        String paymentDate = "";
        double amount = 0.0;
        invoiceObj.setPaymentDate(paymentDate);
        invoiceObj.setAmount(amount);
        ctrInvoice.insertInvoice(invoiceObj);
        System.out.println("Invoice inserted!!!!!!!");

        invoiceNo = GetMax.getMaxId("Select max(invoiceNo) from Invoice");
        Invoice invObj = new Invoice();
        invObj = ctrInvoice.findInvoiceById(invoiceNo);

        CtrCustomer ctrCustomer = new CtrCustomer();
        Customer customerObj = ctrCustomer.findCostumerById(customerId, true);

        CtrSalesOrder ctrSalesOrder = new CtrSalesOrder();
        SalesOrder salesOrderObj = new SalesOrder();
        salesOrderObj.setDate("");
        salesOrderObj.setDeliverDate("");
        salesOrderObj.setDeliveryStatus("");
        salesOrderObj.setInvoiceId(invObj);
        salesOrderObj.setCustomerId(customerObj);
        ctrSalesOrder.insertSalesOrder(salesOrderObj);
        
        salesOrderId = GetMax.getMaxId("Select max(id) from SalesOrder");
    }

    private void enterItem(){
        int partNo = Integer.parseInt(partNoField.getText());
        CtrProduct ctrProd = new CtrProduct();
        CtrSalesLine ctrSalesLine = new CtrSalesLine();
        CtrSalesOrder ctrSalesOrder = new CtrSalesOrder();
        Product prodObj = new Product();
        prodObj = ctrProd.findProductByPartNo(partNo, true);
        int amountInDatabase = ctrProd.findProductByPartNo(partNo, false).getAmount();
        int amount = Integer.parseInt(amountField.getText());
        int amountLeft = amountInDatabase - amount;
        ctrProd.updateProductAmount(partNo, amountLeft);
        double price = prodObj.getSalesPrice();
        double totalPrice = amount * price;
        totalOrderPrice = totalOrderPrice + totalPrice;
        totalOrderPriceLabel.setText(String.valueOf(totalOrderPrice));
        SalesOrder salesOrderObj = ctrSalesOrder.findBySalesOrderId(salesOrderId, true);
        
        SalesLine salesLine = new SalesLine();
        salesLine.setAmount(amount);
        salesLine.setPrice(price);
        salesLine.setTotalPrice(totalPrice);
        salesLine.setSalesOrderId(salesOrderObj);
        salesLine.setProductId(prodObj);
        ctrSalesLine.insertSalesLine(salesLine);
        totalAmount = totalAmount + amount;
        amountLabel.setText("Items: "+totalAmount);
        updateList();
    }

    private void searchCustomer(){
        CtrCustomer ctrCus = new CtrCustomer();
        String input = searchCustIdField.getText().trim();
        if(input.length() == 0){
            input = searchCustName.getText().trim();
            if(input.length() == 0){
                searchCustStatusLabel.setText("Insert id or first name");
            }
            else{
                Customer cusObj = new Customer();
                cusObj = ctrCus.findCostumerByFname(input, true);
                putCustomerValuesOnTheScreen(cusObj);
                customerId = cusObj.getId();
                createNewOrder();
            }
        }
        else{
            Customer cusObj = new Customer();
            cusObj = ctrCus.findCostumerById(Integer.parseInt(input), true);
            putCustomerValuesOnTheScreen(cusObj);
            customerId = cusObj.getId();
            createNewOrder();
        }
        
    }

    private void putCustomerValuesOnTheScreen(Customer cusObj){
        searchCustIdField.setText(String.valueOf(cusObj.getId()));
        searchCustName.setText(cusObj.getFname()+ " " + cusObj.getLname());
    }
    private void printInvoice(){
        JOptionPane.showMessageDialog(null, "This is not implemented yet.", "Not implemented", JOptionPane.PLAIN_MESSAGE);
    }

    private void updateList()
    {
        CtrSalesLine ctrSalesline = new CtrSalesLine();
        ArrayList<SalesLine> salesLine = ctrSalesline.getAllSalesLine(String.valueOf(salesOrderId), true);
        buildList(salesLine);
    }

    private void buildList(ArrayList<SalesLine> salesLines)
    {
        String [] salesLine = new String[salesLines.size()];
        int index = 0;
        for(SalesLine sObj : salesLines)
        {
            String name = sObj.getProductId().getName();
            String price = String.valueOf(sObj.getProductId().getSalesPrice());
            String amount = String.valueOf(sObj.getAmount());
            String totalPrice = String.valueOf(sObj.getProductId().getSalesPrice() * sObj.getAmount());

            if (name.length() > 20){
                name = name.substring(0, 19);
            }
            if(name.length() < 20){
                int length = name.length();
                while (length < 19)
                {
                    name = name + " ";
                    length++;
                }
            }
            if(price.length() < 6){
                int length = price.length();
                while (length <6){
                    price = price + " ";
                    length++;
                }
            }
            if(amount.length() < 1){
                int length = amount.length();
                while(length < 1){
                    amount = amount + " ";
                    length++;
                }
            }

            salesLine[index] = (name + "   " + price + "   " + amount + "           " + totalPrice);
            index++;
        }
        jList1.setListData(salesLine);
    }
}
    

